package com.mobi.Survey;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobi.Survey.controller.SurveyPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomAdapterSurveys.java
 * <p>
 * This class is sub class of BaseAdapter.java and a standard class. Responsible
 * for creating rows of surveys SurveyListScreen.java uses it
 * 
 */
public class CustomAdapterSurveys extends BaseAdapter {
	private LayoutInflater mInflater;
	private Object ctx;
	private List<SurveyPojo> surveys;

	public CustomAdapterSurveys(Object ctx, List<SurveyPojo> surveys) {
		this.surveys = surveys;
		this.ctx = ctx;
		this.mInflater = LayoutInflater.from((Context) ctx);

	}

	@Override
	public int getCount() {
		if (null == surveys)
			return 0;
		return surveys.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	class ViewHolder {

		TextView txtSurveyDesc;
		TextView txtSurveyKey;
		ImageView ivIcon;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {

			convertView = mInflater.inflate(LayoutIds.SURVEY_LIST_ROW, null);

			holder = new ViewHolder();
			holder.txtSurveyDesc = (TextView) convertView
					.findViewById(R.id.txtSurveyDesc);

			holder.txtSurveyKey = (TextView) convertView
					.findViewById(R.id.txtSurveyKey);

			holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txtSurveyKey.setText(surveys.get(position).getSurveyKey());
		holder.txtSurveyDesc.setText(surveys.get(position)
				.getSurveyDiscription());
		if (surveys.get(position).getBitmapIcon() != null) {
			try {
				holder.ivIcon.setImageBitmap(surveys.get(position)
						.getBitmapIcon());
			} catch (OutOfMemoryError e) {
				System.gc();
				Log.e(this.getClass().getSimpleName(),
						"Out of memory due to big icon image");
				holder.ivIcon.setImageResource(R.drawable.icon);
			}

		} else {
			holder.ivIcon.setImageResource(R.drawable.icon);
		}
		// Create alternate colors
		if (position % 2 == 0) {

			convertView.setBackgroundColor(((Context) ctx).getResources()
					.getColor(R.color.LVRowColor1));
		} else {
			convertView.setBackgroundColor(Color.WHITE);
		}

		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				((SurveyListScreen) ctx).onSurveyListClick(surveys
						.get(position).getId());

			}
		});
		return convertView;

	}

	/**
	 * clear all Bitmap icons of survey list resources held by Survey List
	 */
	public void dispose() {
		if (surveys != null) {
			for (SurveyPojo survey : surveys) {
				if (survey.getBitmapIcon() != null
						&& !survey.getBitmapIcon().isRecycled()) {
					survey.getBitmapIcon().recycle();
					survey.setBitmapIcon(null);
				}

			}
			surveys.clear();

		}

	}
}
